﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Transactions;
using IssueTracker.Data;
using IssueTracker.Business.Entity;

namespace IssueTracker.Business.BO
{
    public class IssueBO
    {

  
        private IssueDataAccess da;

        public IssueBO() 
        {
            da = new IssueDataAccess();
        }


#region Business Methods

        /// <summary>
        /// Add a new Issue
        /// </summary>
        /// <param name="issue"></param>
        /// <returns></returns>
        public Issue AddNew(Issue issue)
        {
            Issue returnIssue;

            issue.CreationDate = DateTime.Today;
            issue.CreatorId = System.Threading.Thread.CurrentPrincipal.Identity.Name;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnIssue = da.Create(issue);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }

            return returnIssue;

        }

        public Issue UpdateIssue(Issue issue)
        {
            Issue returnIssue;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnIssue = da.Update(issue);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                
                return returnIssue;
            }
        }

        public void DeleteIssue(Issue issue)
        {
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    da.Delete(issue);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<Issue> ListIssues()
        {
            IssueDataAccess data = new IssueDataAccess();

            IEnumerable<Issue> returnList = data.List();
            return returnList;
        }

        public IEnumerable<Issue> SearchIssues(Issue issue)
        {
            IssueDataAccess data = new IssueDataAccess();

            IEnumerable<Issue> returnList = data.Search(issue);
            return returnList;
        }


        public Issue ListIssueByWorfklowInstanceId(System.Guid workflowInstanceId)
        {
            IssueDataAccess data = new IssueDataAccess();

            Issue issue = data.ListIssueByWorfklowInstanceId(workflowInstanceId);

            return issue;
        }

        public Issue GetIssue(System.Guid id)
        {
            return da.Retrieve(id);
        }

        public IEnumerable<Issue> TaskList(string userName, string stateName)
        {
            //TODO: Should check whether stateName exist in the workflow

            return da.TaskList(userName);
        }
        
#endregion
        
   
    }
}
